/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

/**
 * Base of all the lovely input types for forms.
 * @author J Patrick Davenport
 *
 */
@SuppressWarnings("rawtypes")
public abstract class InputPageElement extends ChildlessPageElement implements IInlineElement, ICoreAttributes, ILanguageAttributes, IKeyboardEvents, IMouseEvents, IFormEvents<InputPageElement> {

	private static final String	VALUE_KEY		= "value";
	private static final String	BLANK			= "";
	private static final String	SIZE_KEY		= "size";
	private static final String	NAME_KEY		= "name";
	private static final String	DISABLED_KEY	= "disabled";
	private static final String	ALT_KEY			= "alt";
	private static final String	INPUT_TYPE		= "input";

	{
		this.setTag(INPUT_TYPE);
		this.setSupportsAttributes(true);
	}

	public InputPageElement() {

	}

	public InputPageElement(final String _value) {
		this.setValue(_value);
	}

	public InputPageElement(final String _name, final String _value) {
		this(_value);
		this.setName(_name);
	}

	public String getAlt() {
		return this.getAttributeValue(ALT_KEY);
	}

	public boolean isDisabled() {
		return DISABLED_KEY.equals(this.getAttributeValue(DISABLED_KEY));
	}

	public String getName() {
		return this.getAttributeValue(NAME_KEY);
	}

	public Integer getSize() {
		final String value = this.getAttributeValue(SIZE_KEY);
		return StringUtils.isEmpty(value) ? null : Integer.valueOf(value);
	}

	public String getValue() {
		return this.getAttributeValue(VALUE_KEY);
	}

	public InputPageElement setAlt(final String _value) {
		this.setAttribute(ALT_KEY, _value);
		return this;
	}

	public InputPageElement setDisabled(final boolean _value) {
		if (_value) {
			this.setAttribute(DISABLED_KEY, DISABLED_KEY);
		} else {
			this.removeAttribute(DISABLED_KEY);
		}
		return this;
	}

	public InputPageElement setName(final String _value) {
		this.setAttribute(NAME_KEY, _value);
		return this;
	}

	public InputPageElement setSize(final Integer _value) {
		this.setAttribute(SIZE_KEY, _value == null ? BLANK : _value.toString());
		return this;
	}

	public InputPageElement setValue(final String _value) {
		this.setAttribute(VALUE_KEY, StringEscapeUtils.escapeHtml(_value));
		return this;
	}

	@Override
	public InputPageElement setOnClick(final String _scriptlet) {
		return (InputPageElement) this.setAttribute(ON_MOUSECLICK_EVENT_KEY, _scriptlet);
	}

	@Override
	public InputPageElement setOnDblClick(final String _scriptlet) {
		return (InputPageElement) this.setAttribute(ON_MOUSEDBLCLICK_EVENT_KEY, _scriptlet);
	}

	@Override
	public InputPageElement setOnMouseDown(final String _scriptlet) {
		return (InputPageElement) this.setAttribute(ON_MOUSEDOWN_EVENT_KEY, _scriptlet);
	}

	@Override
	public InputPageElement setOnMouseMove(final String _scriptlet) {
		return (InputPageElement) this.setAttribute(ON_MOUSEMOVE_EVENT_KEY, _scriptlet);
	}

	@Override
	public InputPageElement setMouseOut(final String _scriptlet) {
		return (InputPageElement) this.setAttribute(ON_MOUSEOUT_EVENT_KEY, _scriptlet);
	}

	@Override
	public InputPageElement setMouseOver(final String _scriptlet) {
		return (InputPageElement) this.setAttribute(ON_MOUSEOVER_EVENT_KEY, _scriptlet);
	}

	@Override
	public InputPageElement setMouseUp(final String _scriptlet) {
		return (InputPageElement) this.setAttribute(ON_MOUSEUP_EVENT_KEY, _scriptlet);
	}

	@Override
	public String getOnClick() {
		return this.getAttributeValue(ON_MOUSECLICK_EVENT_KEY);
	}

	@Override
	public String getOnDblClick() {
		return this.getAttributeValue(ON_MOUSEDBLCLICK_EVENT_KEY);
	}

	@Override
	public String getOnMouseDown() {
		return this.getAttributeValue(ON_MOUSEDOWN_EVENT_KEY);
	}

	@Override
	public String getOnMouseMove() {
		return this.getAttributeValue(ON_MOUSEMOVE_EVENT_KEY);
	}

	@Override
	public String getMouseOut() {
		return this.getAttributeValue(ON_MOUSEOUT_EVENT_KEY);
	}

	@Override
	public String getMouseOver() {
		return this.getAttributeValue(ON_MOUSEOVER_EVENT_KEY);
	}

	@Override
	public String getMouseUp() {
		return this.getAttributeValue(ON_MOUSEUP_EVENT_KEY);
	}

	@Override
	public InputPageElement setOnKeyDown(final String _scriptlet) {
		return (InputPageElement) this.setAttribute(KEY_DOWN_EVENT_KEY, _scriptlet);
	}

	@Override
	public InputPageElement setOnKeyPress(final String _scriptlet) {
		return (InputPageElement) this.setAttribute(KEY_PRESS_EVENT_KEY, _scriptlet);
	}

	@Override
	public InputPageElement setOnKeyUp(final String _scriptlet) {
		return (InputPageElement) this.setAttribute(KEY_UP_EVENT_KEY, _scriptlet);
	}

	@Override
	public String getOnKeyDown() {
		return this.getAttributeValue(KEY_DOWN_EVENT_KEY);
	}

	@Override
	public String getOnKeyPress() {
		return this.getAttributeValue(KEY_PRESS_EVENT_KEY);
	}

	@Override
	public String getOnKeyUp() {
		return this.getAttributeValue(KEY_UP_EVENT_KEY);
	}

	@Override
	public String getCssClass() {
		return this.getAttributeValue(CSS_CLASS_KEY);
	}

	@Override
	public String getId() {
		return this.getAttributeValue(ID_KEY);
	}

	@Override
	public String getStyle() {
		return this.getAttributeValue(STYLE_KEY);
	}

	@Override
	public String getTitle() {
		return this.getAttributeValue(TITLE_KEY);
	}

	@Override
	public InputPageElement setCssClass(final String _value) {
		return (InputPageElement) this.setAttribute(CSS_CLASS_KEY, _value);
	}

	@Override
	public InputPageElement setId(final String _value) {
		return (InputPageElement) this.setAttribute(ID_KEY, _value);
	}

	@Override
	public InputPageElement setStyle(final String _value) {
		return (InputPageElement) this.setAttribute(STYLE_KEY, _value);
	}

	@Override
	public InputPageElement setTitle(final String _value) {
		return (InputPageElement) this.setAttribute(TITLE_KEY, _value);
	}

	@Override
	public InputPageElement setDir(final String _value) {
		return (InputPageElement) this.setAttribute(DIR_KEY, _value);
	}

	@Override
	public InputPageElement setLang(final String _lang) {
		return (InputPageElement) this.setAttribute(LANG_KEY, _lang);
	}

	@Override
	public InputPageElement setXmlLang(final String _xmlLang) {
		return (InputPageElement) this.setAttribute(XML_LANG_KEY, _xmlLang);
	}

	@Override
	public String getDir() {
		return this.getAttributeValue(DIR_KEY);
	}

	@Override
	public String getLang() {
		return this.getAttributeValue(LANG_KEY);
	}

	@Override
	public String getXmlLang() {
		return this.getAttributeValue(XML_LANG_KEY);
	}

	protected void setType(final String _value) {
		this.setAttribute("type", _value);
	}

	@Override
	public InputPageElement setOnBlur(final String _value) {
		return (InputPageElement) this.setAttribute(ON_BLUR_KEY, _value);
	}

	@Override
	public InputPageElement setOnChange(final String _value) {
		return (InputPageElement) this.setAttribute(ON_CHANGE_KEY, _value);
	}

	@Override
	public InputPageElement setOnFocus(final String _value) {
		return (InputPageElement) this.setAttribute(ON_FOCUS_KEY, _value);
	}

	@Override
	public InputPageElement setOnSelect(final String _value) {
		return (InputPageElement) this.setAttribute(ON_SELECT, _value);
	}

	@Override
	public String getOnBlur() {
		return this.getAttributeValue(ON_BLUR_KEY);
	}

	@Override
	public String getOnChange() {
		return this.getAttributeValue(ON_CHANGE_KEY);
	}

	@Override
	public String getOnFocus() {
		return this.getAttributeValue(ON_FOCUS_KEY);
	}

	@Override
	public String getOnSelect() {
		return this.getAttributeValue(ON_SELECT);
	}
}
